<?php
$rss_site = $_POST['rsssite'];

$reader = new XMLReader();
$result = array();
$re = $reader->open($rss_site);

if($re){
	$result['code'] = 'success';
	
	while($reader->read()){
		if($reader->nodeType == XMLReader::ELEMENT){
			$item = false;
			if($reader->name == 'rss' && $reader->nodeType == XMLReader::ELEMENT){
				$item = 'item';
			}else if($reader->name == 'feed' && $reader->nodeType == XMLReader::ELEMENT){
				$item = 'entry';
			}else{
				$result['code'] = 'failure';
				$result['msg'] = '文档不是RSS或FEED！';
			}
			break;
		}
	}
	if ($item){
		while($reader->read() ){
			if($reader->name == $item && $reader->nodeType == XMLReader::ELEMENT){
				$innerxml = $reader->readInnerXml();
				try{
					$xm = new XMLReader();
					$xm->XML('<xml>'.$innerxml.'</xml>');
					$data = array();
					while($xm->read() ){
						if($xm->name != 'xml' &&  $xm->nodeType == XMLReader::ELEMENT)
							$result['data'][$xm->name] = $xm->name;
					}
					$xm->close();
				}catch (Exception $e){
					echo $e->getMessage();
				}
				break;
			}
		}
	}
}else{
	$result['code'] = 'failure';
	$result['msg'] = '地址不正确或内容不是一个XML！';
}

$reader->close();
echo json_encode($result);
?>